<?php
require_once 'vendor/autoload.php';

// 初始化应用
$app = new \think\App();
$app->initialize();

use think\facade\Db;

try {
    echo "扩展orders表...\n";
    
    // 添加新字段
    $alterQueries = [
        "ALTER TABLE orders ADD COLUMN shop_id VARCHAR(50) COMMENT '店铺ID' AFTER platform",
        "ALTER TABLE orders ADD COLUMN shop_name VARCHAR(200) COMMENT '店铺名称' AFTER shop_id",
        "ALTER TABLE orders ADD COLUMN buyer_account VARCHAR(100) COMMENT '买家账号' AFTER customer_name",
        "ALTER TABLE orders ADD COLUMN buyer_user_type INT DEFAULT 0 COMMENT '买家用户类型' AFTER buyer_account",
        "ALTER TABLE orders ADD COLUMN order_status INT DEFAULT 0 COMMENT '原始订单状态' AFTER status",
        "ALTER TABLE orders ADD COLUMN order_status_str VARCHAR(50) COMMENT '订单状态描述' AFTER order_status",
        "ALTER TABLE orders ADD COLUMN pay_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '实付金额' AFTER total_amount",
        "ALTER TABLE orders ADD COLUMN promotion_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '优惠金额' AFTER pay_amount",
        "ALTER TABLE orders ADD COLUMN post_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '运费' AFTER promotion_amount",
        "ALTER TABLE orders ADD COLUMN pay_time DATETIME NULL COMMENT '支付时间' AFTER post_amount",
        "ALTER TABLE orders ADD COLUMN ship_time DATETIME NULL COMMENT '发货时间' AFTER pay_time",
        "ALTER TABLE orders ADD COLUMN confirm_receipt_time DATETIME NULL COMMENT '确认收货时间' AFTER ship_time",
        "ALTER TABLE orders ADD COLUMN seller_words TEXT COMMENT '卖家备注' AFTER confirm_receipt_time",
        "ALTER TABLE orders ADD COLUMN buyer_words TEXT COMMENT '买家备注' AFTER seller_words",
        "ALTER TABLE orders ADD COLUMN tracking_number VARCHAR(100) COMMENT '物流单号' AFTER buyer_words",
        "ALTER TABLE orders ADD COLUMN logistics_name VARCHAR(100) COMMENT '物流公司' AFTER tracking_number",
        "ALTER TABLE orders ADD COLUMN logistics_code VARCHAR(50) COMMENT '物流编码' AFTER logistics_name",
        "ALTER TABLE orders ADD COLUMN receiver_name VARCHAR(100) COMMENT '收货人姓名' AFTER logistics_code",
        "ALTER TABLE orders ADD COLUMN receiver_phone VARCHAR(50) COMMENT '收货人电话' AFTER receiver_name",
        "ALTER TABLE orders ADD COLUMN receiver_address TEXT COMMENT '收货地址' AFTER receiver_phone",
        "ALTER TABLE orders ADD COLUMN province VARCHAR(50) COMMENT '省份' AFTER receiver_address",
        "ALTER TABLE orders ADD COLUMN city VARCHAR(50) COMMENT '城市' AFTER province",
        "ALTER TABLE orders ADD COLUMN district VARCHAR(50) COMMENT '区县' AFTER city",
        "ALTER TABLE orders ADD COLUMN street VARCHAR(100) COMMENT '街道' AFTER district",
        "ALTER TABLE orders ADD COLUMN after_sale_status INT DEFAULT 0 COMMENT '售后状态' AFTER street",
        "ALTER TABLE orders ADD COLUMN after_sale_type INT DEFAULT 0 COMMENT '售后类型' AFTER after_sale_status",
        "ALTER TABLE orders ADD COLUMN after_sale_reason VARCHAR(200) COMMENT '售后原因' AFTER after_sale_type",
        "ALTER TABLE orders ADD COLUMN refund_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '退款金额' AFTER after_sale_reason",
        "ALTER TABLE orders ADD COLUMN goods_id VARCHAR(100) COMMENT '商品ID' AFTER refund_amount",
        "ALTER TABLE orders ADD COLUMN sku_id VARCHAR(100) COMMENT 'SKU ID' AFTER goods_id",
        "ALTER TABLE orders ADD COLUMN goods_spec TEXT COMMENT '商品规格' AFTER sku_id",
        "ALTER TABLE orders ADD COLUMN goods_img VARCHAR(500) COMMENT '商品图片' AFTER goods_spec",
        "ALTER TABLE orders ADD COLUMN mall_id VARCHAR(100) COMMENT '店铺ID' AFTER goods_img",
        "ALTER TABLE orders ADD COLUMN parent_order_id VARCHAR(100) COMMENT '父订单ID' AFTER mall_id",
        "ALTER TABLE orders ADD COLUMN channel_payment_no VARCHAR(100) COMMENT '支付流水号' AFTER parent_order_id",
        "ALTER TABLE orders ADD COLUMN pay_type VARCHAR(20) COMMENT '支付方式' AFTER channel_payment_no",
        "ALTER TABLE orders ADD COLUMN platform_cost_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '平台费用' AFTER pay_type",
        "ALTER TABLE orders ADD COLUMN commission_price DECIMAL(10,2) DEFAULT 0.00 COMMENT '佣金' AFTER platform_cost_amount",
        "ALTER TABLE orders ADD COLUMN is_auto TINYINT(1) DEFAULT 0 COMMENT '是否自动处理' AFTER commission_price",
        "ALTER TABLE orders ADD COLUMN is_encrypt_order TINYINT(1) DEFAULT 0 COMMENT '是否加密订单' AFTER is_auto",
        "ALTER TABLE orders ADD COLUMN sync_record_id INT DEFAULT 0 COMMENT '同步记录ID' AFTER is_encrypt_order"
    ];
    
    foreach ($alterQueries as $query) {
        try {
            Db::execute($query);
            echo "执行: " . substr($query, 0, 50) . "... 成功\n";
        } catch (Exception $e) {
            echo "执行: " . substr($query, 0, 50) . "... 失败: " . $e->getMessage() . "\n";
        }
    }
    
    echo "\norders表扩展完成\n";
    
    // 检查表结构
    $columns = Db::query('DESCRIBE orders');
    echo "\norders表字段总数: " . count($columns) . "\n";
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
} 